<?php

namespace App\Models;

use App\Common\Constant\Params;
use App\Services\IStorageService;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class OrderRefund extends BaseModel
{

    protected $table = 'orders_refund';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'upd_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'order_number',
        'order_id',
        'refund_reason',
        'refund_time',
        'refund_type',
        'refund_amount',
        'refund_method',
        'refund_result_time',
        'refund_result_type',
        'refund_refuse_reason',
        'arbitrate_time',
        'arbitrate_name',
        'arbitrate_phone',
        'arbitrate_reason',
        'arbitrate_imgs',
        'arbitrate_result_time',
        'arbitrate_result_type',
        'arbitrate_result_amount',
        'arbitrate_result_reason',
        'refund_success_time',
    ];

    protected $casts = [
        'refund_time' => 'datetime',
        'refund_amount' => 'double',
        'refund_result_time' => 'datetime',
        'arbitrate_time' => 'datetime',
        'arbitrate_result_time' => 'datetime',
        'arbitrate_result_amount' => 'double',
        'refund_success_time' => 'datetime',
        'arbitrate_imgs' => 'array',
    ];

    protected $touches = ['order'];

    public function order(){
        return $this->belongsTo(Order::class,'order_id');
    }

    /**
     * 退款金额在师傅端显示
     * master_refund_amount
     */
    public function getMasterRefundAmountAttribute()
    {
        $master_money = $this->order->master_money;
        $system_money = $this->order->system_money;
        if ($this->refund_amount >= $master_money){
            return $this->refund_amount - $system_money;
        }else{
            return $this->refund_amount;
        }
    }

    /**
     * 仲裁退款金额在师傅端显示
     * master_arbitrate_amount
     */
    public function getMasterArbitrateAmountAttribute()
    {
        if (!$this->arbitrate_result_amount){
            return;
        }
        $master_money = $this->order->master_money;
        $system_money = $this->order->system_money;
        if ($this->arbitrate_result_amount >= $master_money){
            return $this->arbitrate_result_amount - $system_money;
        }else{
            return $this->arbitrate_result_amount;
        }
    }

    /**
     * 退款后师傅还剩余多少要打入余额
     * master_remain
     */
    public function getMasterRemainAttribute()
    {
        if ($this->master_arbitrate_amount){
            return $this->order->master_money - $this->master_arbitrate_amount;
        }else{
            return $this->order->master_money - $this->master_refund_amount;
        }
    }

    /**
     * 退款类型文本
     */
    public function getRefundTypeTextAttribute()
    {
        return Params::$ORDER_REFUND_TYPE[$this->refund_type];
    }

    /**
     * 系统默认自动退款截止时间
     * auto_refund_endtime
     */
    public function getAutoRefundEndtimeAttribute()
    {
        $end_time = $this->refund_time->addDays(config('common.auto_refund_days'));
        if ($end_time->lte(Carbon::now())){
            return $end_time->format('Y-m-d H:i:s');
        }

        $rest_time = $end_time->timestamp - time();
        $text = '';
        $days = intval($rest_time/86400);
        if ($days){
            $text .= "{$days}天";
            $rest_time -= 86400*$days;
        }
        $hours = intval($rest_time/3600);
        if ($hours){
            $text .= "{$hours}小时";
            $rest_time -= 3600*$hours;
        }
        $minutes = intval($rest_time/60);
        if ($minutes){
            $text .= "{$minutes}分";
        }else if (empty($text)){
            $text = ' < 1分钟';
        }
        return $text;
    }

    /**
     * 退款申请中
     * refunding
     */
    public function getRefundingAttribute()
    {
        return !empty($this->refund_time) && empty($this->refund_result_time);
    }

    /**
     * 客服仲裁中
     * arbitrating
     */
    public function getArbitratingAttribute()
    {
        return !empty($this->arbitrate_time) && empty($this->arbitrate_result_time);
    }

    /**
     * 判断退款操作结果是否同意退款
     * refund_agree
     */
    public function getRefundAgreeAttribute()
    {
        return $this->refund_result_type == Params::ORDER_REFUND_RESULT_AGREE;
    }

    /**
     * 判断退款操作结果是否不同意退款
     * refund_disagree
     */
    public function getRefundDisagreeAttribute()
    {
        return $this->refund_result_type == Params::ORDER_REFUND_RESULT_DISAGREE;
    }

    /**
     * 判断仲裁结果是否同意退款
     * arbitrate_agree
     */
    public function getArbitrateAgreeAttribute()
    {
        return in_array($this->arbitrate_result_type,[Params::ORDER_ARBITRATE_RESULT_AGREE_FULL,Params::ORDER_ARBITRATE_RESULT_AGREE_PART]);
    }

    /**
     * 判断仲裁结果是否不同意退款
     * arbitrate_disagree
     */
    public function getArbitrateDisagreeAttribute()
    {
        return $this->arbitrate_result_type == Params::ORDER_ARBITRATE_RESULT_DISAGREE;
    }

    /**
     * 仲裁结果类型文本
     * refund_result_type_text
     */
    public function getRefundResultTypeTextAttribute()
    {
        return Params::$ORDER_REFUND_RESULT[$this->refund_result_type];
    }

    /**
     * 仲裁结果类型文本
     * arbitrate_result_type_text
     */
    public function getArbitrateResultTypeTextAttribute()
    {
        return @Params::$ORDER_ARBITRATE_RESULT[$this->arbitrate_result_type];
    }

    /**
     * 申请仲裁证据图片url
     * arbitrate_imgs_url
     */
    public function getArbitrateImgsUrlAttribute()
    {
        return Collection::make($this->arbitrate_imgs)->transform(function($item,$key){
            return resolve(IStorageService::class)->getUrl($item);
        });
    }

    /**
     * 退款退还方式文本
     * refund_method_text
     */
    public function getRefundMethodTextAttribute()
    {
        return Params::$REFUND_METHOD[$this->refund_method];
    }

}
